package com.fingard.dsp.bank.directbank.bocom01;

import com.bocom.jump.bp.util.Base64;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.ReqQryFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfoDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取还款信息
 * 查询有三个规则：1、直接送三方协议；2、经销商证件号和类型+主机厂证件号和类型；3、特约店代码+主机厂证件号和类型
 *
 * @author marlon
 * @date 2019/1/15
 */
public class BOCOM01QryFinanceRecInfo extends BOCOM01Base implements IFinanceRecInfo {


    @Override
    public RetAtsFinanceRecInfo qryFinanceRecInfo(ReqQryFinanceRecInfo recInfo) throws Exception {
        RetAtsFinanceRecInfo retAts = new RetAtsFinanceRecInfo(recInfo.reqHeader);
        retAts.writeStart();
        try {
            String page = "0";
            int index = 0;
            //分页查询
            while ("0".equals(page)) {
                tr_code = "queryCustRepaymentInfo";
                req_no = getQryTransID();
                XmlTextWriter xmlWriter = new XmlTextWriter();
                // 写入通用的头部节点
                writeCommonHeader2(xmlWriter);

                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("BANK_ID", ""));
                tmpXElement.addChild(new XElement("TRIARG_ID", ""));
                tmpXElement.addChild(new XElement("CUST_ID_NO", getBankConfig().getValueAsString("custidno")));
                tmpXElement.addChild(new XElement("ID_TYPE", getBankConfig().getValueAsString("idtype")));
                tmpXElement.addChild(new XElement("ERP_CUST_ID", recInfo.DealerCode));
                //主机厂证件号码
                tmpXElement.addChild(new XElement("SV_CUST_ID_NO", getBankConfig().getValueAsString("svcustidno")));
                //主机厂证件类型
                tmpXElement.addChild(new XElement("SV_ID_TYPE", getBankConfig().getValueAsString("svidtype")));
                //融资产品类型 01-银票（电票、纸票），02-流贷
                tmpXElement.addChild(new XElement("FINANCING_TYPE",getBankConfig().getValueAsString("financingtype")));//getBankConfig().getValueAsString("financingtype")
                //起始时间
                tmpXElement.addChild(new XElement("START_DATE", Format.Date10Format.format(Format.Date8Format.parse(recInfo.BeginDate))));
                //截止时间
                tmpXElement.addChild(new XElement("END_DATE", Format.Date10Format.format(Format.Date8Format.parse(recInfo.EndDate))));
                tmpXElement.addChild(new XElement("BACKUP_FIELD", ""));
                tmpXElement.addChild(new XElement("START_INDEX", String.valueOf(index)));
                tmpXElement.addChild(new XElement("PAGE_SIZE", "30"));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();
                WriteBankLogStep2(xmlWriter.toXmlString());

                XmlTextWriter xmlWriters = new XmlTextWriter();// 发银行报文的XML对象
                tr_code = "FHTS01";
                writeCommonHeader(xmlWriters);
                xmlWriters.writeStartElement("body");
                xmlWriters.writeElementClosed("rem");
                xmlWriters.writeElementString("systemId", "SCF");
                xmlWriters.writeElementString("data", Base64.encode(xmlWriter.toXmlString().getBytes(getCharset())));
                xmlWriters.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriters.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    // 解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String ans_code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(ans_code)) {
                            String particular_code = tmpBkReader.getFirstText("/ap/head/particular_code");
                            String particular_info = tmpBkReader.getFirstText("/ap/head/particular_info");
                            if ("0000".equals(particular_code)) {
                                String body = new String(Base64.decode(tmpBkReader.getFirstText("/ap/body/RspMsgContent")));
                                WriteBankLogStep3(body);
                                StringBuilder bodyMsg = new StringBuilder();
                                // 解析银行返回
                                XmlTextReader bodyBkReader = loadBankRespXmlReader(body, bodyMsg);
                                if (bodyBkReader != null) {
                                    String rsp_status = bodyBkReader.getFirstText("/mes/head/RSP_STATUS");
                                    String rsp_code = bodyBkReader.getFirstText("/mes/head/RSP_CODE");
                                    String rsp_msg = bodyBkReader.getFirstText("/mes/head/RSP_MSG");
                                    if ("N".equals(rsp_status)) {
                                        String end_flag = bodyBkReader.getFirstText("/mes/body/END_FLAG");
                                        if ("0".equals(end_flag)) {
                                            page = "0";
                                        } else {
                                            page = "1";
                                            retAts.writeRespCode("0");
                                            retAts.writeRespInfo(rsp_msg);
                                        }
                                        String DealerCode = bodyBkReader.getFirstText("/mes/body/ERP_CUST_ID");
                                        while (bodyBkReader.read("/mes/body/REPAYMENT_LIST/REPAYMENT")) {
                                            RetAtsFinanceRecInfoDetail tmpRetDetail = new RetAtsFinanceRecInfoDetail();
                                            tmpRetDetail.DealerCode = DealerCode;
                                            tmpRetDetail.VIN = bodyBkReader.readedXElement.getSingleText("FRAME_NUM");
                                            tmpRetDetail.RepaymentDate = bodyBkReader.readedXElement.getSingleText("REPAYMENT_DATE");
                                            tmpRetDetail.RepaymentMoney = bodyBkReader.readedXElement.getSingleText("REPAYMENT_AMT");
                                            tmpRetDetail.CarMortgageTime = Format.Date10Format.format(Format.DateTimeFormat.parse(bodyBkReader.readedXElement.getSingleText("REPAYMENT_TIME")));
                                            tmpRetDetail.Reserve1 = "";
                                            tmpRetDetail.Reserve2 = "";
                                            tmpRetDetail.Reserve3 = "";
                                            tmpRetDetail.Reserve4 = "";
                                            tmpRetDetail.Reserve5 = "";
                                            retAts.writeRetDetail(tmpRetDetail);
                                            index++;
                                        }
                                    } else {
                                        retAts.writeRespCode("-1");
                                        retAts.writeRespInfo(rsp_code + rsp_msg);
                                        break;
                                    }
                                } else {
                                    retAts.writeRespCode("-1");
                                    retAts.writeRespInfo("返回报文为空！");
                                    break;
                                }
                            } else {
                                retAts.writeRespCode("-1");
                                retAts.writeRespInfo(particular_info);
                                break;
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(ans_info);
                            break;
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                        break;
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                    break;
                }
            }
            WriteBankLogLn2("获取还款信息条数为：" + index + "条！");
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;
    }
}
